package model;

import java.awt.Color;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

public class GameServer
{
	final int PORT_NUMBER = 10055;
	final int MAX_PLAYERS = 4;
	ServerSocket serverSocket;
	Socket client;
	int connectedPlayers = 0;
	ArrayList<Thread> allPlayersSockets;
	ArrayList<Integer> playerCommands;
	ArrayList<GameTile> gameTiles;
	GameQueue gameQueue;
	GameBoard gameBoard;
	GameSocketThread gst;
	Monster monster;

	public GameServer()
	{
		monster = new Monster();
		setup();
		serverStart();
	}

	public void setup()
	{
		try
		{// establish server socket to listen to connections. with maximum
			// players.
			serverSocket = new ServerSocket(PORT_NUMBER, MAX_PLAYERS);
			serverSocket.setPerformancePreferences(0, 1, 0);
			allPlayersSockets = new ArrayList<Thread>();
			playerCommands = new ArrayList<Integer>();
			gameTiles = new ArrayList<GameTile>();
			gameBoard = new GameBoard(); 
			gameQueue = new GameQueue();

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	// generating game board
	public void serverStart()
	{
		gst = new GameSocketThread(allPlayersSockets, playerCommands,
				gameTiles, gameQueue);
		gst.start();
		try
		{
			for (int i = 0; i < 9; i++)
			{
				for (int j = 0; j < 9; j++)
				{
					boolean isStage = true;
					switch (i)
					{
					case 0:
						break;
					case 1:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 2:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 3:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 4:
						break;
					case 5:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 6:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 7:
						if (j != 0 && j != 4 && j != 8)
							isStage = false;
						break;
					case 8:
						break;
					}

					if (isStage == true)
					{
						gameTiles.add(new GameTile(true, false, i, j));
						try
						{
							gameBoard.addTilebyCoordinate(new GameTile(true,
									false, i, j));
						}
						catch (Exception e)
						{
							e.printStackTrace();
						}
						// System.out.println("isStage: " +i+","+j);
					}
					else
					{
						gameTiles.add(new GameTile(false, false, i, j));
						// System.out.println("notStage: " +i+","+j);
					}

				}
			}
			gameQueue.setPlayers(0);
			// gives the monster the reference to the game tiles
			monster.gameBoard.setBoardUsingList(gameTiles);

			while (connectedPlayers < MAX_PLAYERS)
			{
				System.out.println("Waiting on Client");
				client = serverSocket.accept();
				System.out.println("Client connected");

				++connectedPlayers;
				System.out.println("Player " + connectedPlayers + " Connected");

				synchronized (allPlayersSockets)
				{
					Thread t = new GameSocket(client, connectedPlayers,
							gameTiles, gameQueue);
					t.start();
					allPlayersSockets.add(t);
					System.out.println("New thread added " + t);
					System.out.println("Total Threads : " + allPlayersSockets);
				}
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public void gamePlay()
	{
		while (true)
		{
			synchronized (playerCommands)
			{
				for (Iterator<Integer> iter = playerCommands.iterator(); iter
						.hasNext();)
				{
					int move = iter.next();
					applyMove(move);
				}
			}
		}
	}

	public void applyMove(int move)
	{
	}

	public int getNumPlayers()
	{
		return connectedPlayers;
	}
}
